package com.financing.service.agent;
import java.util.Arrays;
import java.util.Map;

import com.financing.model.agent.Visit;
import com.financing.model.user.Staff;
import com.financing.service.BaseService;
import com.financing.service.user.StaffService;
import com.financing.util.Str;
import com.jfinal.plugin.activerecord.Page;

public class VisitService extends BaseService<Visit>{
	private StaffService staff = new StaffService();

	
	/**
	 * 分页信息
	 * @return
	 */
	public Page<Visit> list(int page, int size, Map<String, String> map) {
		return Visit.me.paginate(page, size, buildSql("visit",Arrays.asList("is_solve","create_time","real_name","user_id","type","agent_id"),map));
	}
	
	/**
	 * 生成旗下经纪人
	public String getAgents3(int userId){
		String agents = "";
		Staff u = staff.findByUserId(userId);
		if(Str.eq("admin", u.getRole())) {
			for(Staff s : Staff.me.find("SELECT user_id FROM staff WHERE role='agent' or role='agent_manager' or role='agent_leader'")) {
				agents += s.getUserId()+",";
			}
		} else {
			for(Staff s : Staff.me.find("SELECT user_id FROM staff WHERE role='agent' and (leaders like ? or user_id=?) ", "%,"+userId+",%", userId)) {
				agents += s.getUserId()+",";
			}
		}
		return agents.length()>0 ? agents.substring(0, agents.length()-1) : agents;
	}
	 */
	
	public String getAgents(int userId){
		String agents = "*";
		Staff u = staff.findByUserId(userId);
		if(u!=null) {
			if(Str.eq("admin", u.getRole())) {
				return agents;
			} else {
				for(Staff s : Staff.me.find("SELECT role, user_id FROM staff WHERE leaders like ? or user_id=? ", "%,"+userId+",%", userId)) {
					if(Str.eq("agent", s.getRole()) || Str.eq("agent_leader", s.getRole()) || Str.eq("agent_manager", s.getRole()))
					agents += s.getUserId()+"*";
				}
			}
			return agents;
		}else {
			return "0";
		}
	}
	
	/**
	 * 生成下属
	 */
	public String getSubordinate(int userId){
		String agents = "";
		for(Staff s : Staff.me.find("SELECT user_id FROM staff WHERE leaders like ? ", "%,"+userId+",%")) {
			agents += s.getUserId()+",";
		}
		return agents.length()>0 ? agents.substring(0, agents.length()-1) : agents;
	}
}
